package com.zzyl.common.utils.ai;

import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.core.auth.Auth;
import com.baidubce.qianfan.model.chat.ChatResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * @author robin
 * @date 25.5.23 11:41
 * 百度大模型调用组件对象
 * @RequiredArgsConstructor作用：提供有参构造
 */
@Component
@RequiredArgsConstructor
public class AIModelInvoker {
    private final BaiduAIProperties baiduAIProperties;
    
    /**
     * 对千帆的文本对话接口封装
     * @param prompt 提示词，需要开发者提供
     * @return AI大模型推理结果
     */
    public String chatCompletion(String prompt) {
        Qianfan qianfan=new Qianfan(Auth.TYPE_OAUTH,baiduAIProperties.getAccessKey(),baiduAIProperties.getSecretKey());
        
        // 指定模型
        ChatResponse resp=qianfan.chatCompletion()
                                 .model(baiduAIProperties.getModelName())
                                 .addMessage("user",prompt)
                                 .temperature(0.7)
                                 .maxOutputTokens(2000)
                                 .responseFormat("json_object")
                                 .execute();
        return resp.getResult();
    }
    
}
